﻿using System;
using System.Collections.Generic;
using System.Text;
using PPAP.Model;
using System.Data;
using System.Data.OracleClient;

namespace PPAP.DAO
{
    public class PPARTaskDAO
    {
        public PPARTaskDAO() { }

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(PPARTask model)
        {
            model.ID = DbHelperOra.GetMaxID("ID", "PPARTASK");
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into PPARTASK(");
            strSql.Append("ID,PID,REAMRK,CREATEUSER,CREATETIME)");
            strSql.Append(" values (");
            strSql.Append(":ID,:PID,:REAMRK,:CREATEUSER,:CREATETIME)");
            OracleParameter[] parameters = {
					new OracleParameter(":ID", OracleType.Number,4),
					new OracleParameter(":PID", OracleType.Number,4),
					new OracleParameter(":REAMRK", OracleType.VarChar,500),
					new OracleParameter(":CREATEUSER", OracleType.VarChar,50),
					new OracleParameter(":CREATETIME", OracleType.DateTime)};
            parameters[0].Value = model.ID;
            parameters[1].Value = model.PID;
            parameters[2].Value = model.REAMRK;
            parameters[3].Value = model.CREATEUSER;
            parameters[4].Value = model.CREATETIME;

            return DbHelperOra.ExecuteSql(strSql.ToString(), parameters);
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public int Update(PPARTask model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update PPARTASK set ");
            strSql.Append("ID=:ID,");
            strSql.Append("PID=:PID,");
            strSql.Append("REAMRK=:REAMRK,");
            strSql.Append("CREATEUSER=:CREATEUSER,");
            strSql.Append("CREATETIME=:CREATETIME");
            strSql.Append(" where id="+model.ID);
            OracleParameter[] parameters = {
					new OracleParameter(":ID", OracleType.Number,4),
					new OracleParameter(":PID", OracleType.Number,4),
					new OracleParameter(":REAMRK", OracleType.VarChar,500),
					new OracleParameter(":CREATEUSER", OracleType.VarChar,50),
					new OracleParameter(":CREATETIME", OracleType.DateTime)};
            parameters[0].Value = model.ID;
            parameters[1].Value = model.PID;
            parameters[2].Value = model.REAMRK;
            parameters[3].Value = model.CREATEUSER;
            parameters[4].Value = model.CREATETIME;

            return DbHelperOra.ExecuteSql(strSql.ToString(), parameters);
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public int Delete(int id)
        {
            //该表无主键信息，请自定义主键/条件字段
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from PPARTASK ");
            strSql.Append(" where id="+id);
            OracleParameter[] parameters = {
};

           return  DbHelperOra.ExecuteSql(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public PPARTask GetModel(int id)
        {
            //该表无主键信息，请自定义主键/条件字段
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ID,PID,REAMRK,CREATEUSER,CREATETIME from PPARTASK ");
            strSql.Append(" where id="+id);
            OracleParameter[] parameters = {
};

            PPARTask model = new PPARTask();
            DataSet ds = DbHelperOra.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["ID"].ToString() != "")
                {
                    model.ID = int.Parse(ds.Tables[0].Rows[0]["ID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["PID"].ToString() != "")
                {
                    model.PID = int.Parse(ds.Tables[0].Rows[0]["PID"].ToString());
                }
                model.REAMRK = ds.Tables[0].Rows[0]["REAMRK"].ToString();
                model.CREATEUSER = ds.Tables[0].Rows[0]["CREATEUSER"].ToString();
                if (ds.Tables[0].Rows[0]["CREATETIME"].ToString() != "")
                {
                    model.CREATETIME = DateTime.Parse(ds.Tables[0].Rows[0]["CREATETIME"].ToString());
                }
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select t.*,rownum from (select ID,PID,REAMRK,CREATEUSER,CREATETIME ");
            strSql.Append(" FROM PPARTASK ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" )t order by id");
            return DbHelperOra.Query(strSql.ToString());
        }
    }
}
